package pers.itqiqi.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import pers.itqiqi.dao.UserDao;
import pers.itqiqi.pojo.User;

import java.util.List;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description test_user表
 * @date 2022-07-07 10:25
 */

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 添加用户
     */
    @Override
    public void addUser(User user) {
        String sql = "insert into test.test_user values (?, ?, ?)";
        jdbcTemplate.update(sql, user.getUserId(), user.getUsername(), user.getUstatus());
    }

    /**
     * 修改用户
     */
    @Override
    public void updateUser(User user) {
        String sql = "update test.test_user set userName=?,ustatus=? where userId=?";
        jdbcTemplate.update(sql, user.getUsername(), user.getUstatus(),user.getUserId());
    }

    /**
     * 删除用户
     */
    @Override
    public void deleteUser(String id) {
        String sql = "delete from test.test_user where userId=?";
        jdbcTemplate.update(sql, id);
    }

    /**
     * 查询记录数
     */
    @Override
    public Integer numberOfQueryRecords() {
        String sql = "select count(*) from test.test_user";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    /**
     * 查询指定用户
     * @return user用户类
     */
    @Override
    public User queryUser(String id) {
        String sql = "select * from test.test_user where userId = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
    }

    /**
     * 查询全部用户
     * @return List<user>用户类
     */
    @Override
    public List<User> queryUserAll() {
        String sql = "select * from test.test_user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
    }

    /**
     * 批量增加
     * @param user 用户列表
     */
    @Override
    public void batchAdd(List<Object[]> user) {
        String sql = "insert into test.test_user values (?, ?, ?)";
        jdbcTemplate.batchUpdate(sql, user);
    }

    /**
     * 批量修改
     * @param user 用户列表
     */
    @Override
    public void batchUpdate(List<Object[]> user) {
        String sql = "update test.test_user set userName=?,ustatus=? where userId=?";
        jdbcTemplate.batchUpdate(sql, user);
    }

    /**
     * 批量删除
     * @param id id列表
     */
    @Override
    public void batchDelete(List<Object[]> id) {
        String sql = "delete from test.test_user where userId=?";
        jdbcTemplate.batchUpdate(sql, id);
    }


}
